import pandas
from tkinter import messagebox
from Model import *
from View import *

class Cleanctrllor:
    def __init__(self):
        cleanPreviewButton.configure(command=self.preview)
        cleanSaveButton.configure(command=self.save)

    def clean(self):
        excelCleanResult.createexcel(df=excel0.df)
        excelCleanResult.clean()
        excelCleanResult.resetindex()
        self.state = True

    def preview(self):
        self.clean()
        cleanPreviewTable.loaddata(excelCleanResult.df)
        cleanPreviewWindow.open()

    def save(self):
        self.clean()
        if excel0.ftype == 'xlsx':
            name = '清除空行_' + excel0.fname[:] + '.xlsx'
        else:
            name = '清除空行_' + excel0.fname[:].rpartition('.')[0] + '.xlsx'
        ExcelSaver.setinitialfile(name)
        try:
            path = ExcelSaver.save()[1]
            print(excelCleanResult.df)
            excelCleanResult.saveexcel(path)
            messagebox.showinfo("保存提示","保存成功！")
            cleanMenu.focus_force()
        except:
            messagebox.showinfo("地址无效警告",
                                "请重新选择保存位置，并填写正确的文件后缀名")
            cleanMenu.focus_force()


# 清除空行界面
cleancontroller = Cleanctrllor()
cleanPreviewButton.configure(command=cleancontroller.preview)
cleanSaveButton.configure(command=cleancontroller.save)


